//
//  Pickers.swift
//  learn-ios
//
//  Created by Dong on 2022/4/5.
//

import SwiftUI

struct DarkBlueShadowProgressViewStyle: ProgressViewStyle {
    func makeBody(configuration: Configuration) -> some View {
        ProgressView(configuration)
            .shadow(color: Color(red: 0, green: 0, blue: 0.6),
                    radius: 4.0, x: 1.0, y: 2.0)
    }
}

struct Pickers: View {
    @State private var selectionFlavor: Flavor = .chocolate
    @State private var selectionDate: Date = .init()
    @State private var selectionColor = Color(.sRGB, red: 0.98, green: 0.9, blue: 0.2)
    @State private var progress = 0.5

    let dateRange: ClosedRange<Date> = {
        // 当前日期
        let calendar = Calendar.current

        // 启动组件
        let startComponents = DateComponents(year: 2021, month: 1, day: 1)

        // 结束组件
        let endComponents = DateComponents(year: 2021, month: 12, day: 31, hour: 23, minute: 59, second: 59)

        return calendar.date(from: startComponents)! ... calendar.date(from: endComponents)!
    }()

    enum Flavor: String, CaseIterable, Identifiable {
        case chocolate, vanilla, strawberry
        var id: Self { self }
    }

    var body: some View {
        VStack {
            List {
                NavigationLink {
                    Group {
                        // 单选
                        Picker("Flavor", selection: $selectionFlavor) {
                            Text("Chocolate").tag(Flavor.chocolate)
                            Text("Vanilla").tag(Flavor.vanilla)
                            Text("Strawberry").tag(Flavor.strawberry)
                        }
                        .padding()

                        // 遍历枚举生成选择列表
                        Picker("Flavor", selection: $selectionFlavor) {
                            ForEach(Flavor.allCases) { item in
                                Text(item.rawValue.description)
                            }
                        }

                        // 结合List使用（在子页面选择）
                        List {
                            Picker("Flavor", selection: $selectionFlavor) {
                                ForEach(Flavor.allCases) { item in
                                    Text(item.rawValue.description)
                                }
                            }
                        }

                        // 设置选择器样式
                        Group {
                            // 滚动选择
                            Picker("Flavor", selection: $selectionFlavor) {
                                ForEach(Flavor.allCases) { item in
                                    Text(item.rawValue.description)
                                }
                            }
                            .pickerStyle(.inline)

                            // 菜单选择(默认)
                            Picker("Flavor", selection: $selectionFlavor) {
                                ForEach(Flavor.allCases) { item in
                                    Text(item.rawValue.description)
                                }
                            }
                            .pickerStyle(.menu)
                            .hidden()

                            // 分段选择
                            Picker("Flavor", selection: $selectionFlavor) {
                                ForEach(Flavor.allCases) { item in
                                    Text(item.rawValue.description)
                                }
                            }
                            .pickerStyle(.segmented)
                        }
                    }
                } label: {
                    Text("文本选择器")
                }

                NavigationLink {
                    Group {
                        // 时间选择器
                        DatePicker(selection: $selectionDate) {
                            Text("选择时间")
                        }

                        // 设置显示组件
                        // displayedComponent: .date: 只显示日期 .hourAndMinute: 只显示小时和分钟
                        DatePicker("选择日期",
                                   selection: $selectionDate,
                                   displayedComponents: [.date])
                        DatePicker("选择日期",
                                   selection: $selectionDate,
                                   displayedComponents: [.hourAndMinute])

                        // 限定时间范围
                        DatePicker("选择日期",
                                   selection: $selectionDate,
                                   in: dateRange,
                                   displayedComponents: [.date, .hourAndMinute])

                        // 直接显示日历
                        DatePicker("日历",
                                   selection: $selectionDate)
                            .datePickerStyle(.graphical)
                    }
                } label: {
                    Text("时间选择器")
                }

                NavigationLink {
                    selectionColor
                    ColorPicker("选择颜色: \(selectionColor.description)", selection: $selectionColor)
                        .padding()
                } label: {
                    Text("颜色选择器")
                }

                NavigationLink {
                    // loading动画
                    ProgressView().padding()

                    // 进度条
                    Text("progress: \(progress)")
                    ProgressView(value: progress, total: 10)
                    Button("More", action: {
                        progress += 1
                        if progress >= 10 { progress = 0 }
                    })

                    // 自定义样式
                    ProgressView(value: progress, total: 10)
                        .progressViewStyle(DarkBlueShadowProgressViewStyle())
                } label: {
                    Text("进度条")
                }
            }
            
        }
    }
}

struct Pickers_Previews: PreviewProvider {
    static var previews: some View {
        
        NavigationView {
            Pickers()
        }
    }
}
